package com.wzq.animal.fore.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzq.animal.back.pojo.User;
import com.wzq.animal.common.Result;
import com.wzq.animal.fore.pojo.AnimalInfo;
import com.wzq.animal.fore.pojo.Notice;
import com.wzq.animal.fore.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/notice")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @GetMapping("/getNotice")
    public Result getNotice(){
        List<Notice> notices = noticeService.list();
        return Result.success(notices);
    }

    /**
     * 获取通知分页数据
     * @param currentPage
     * @param pageSize
     * @param authorKeywords
     * @return
     */
    @GetMapping("/getPages/{currentPage}/{pageSize}")
    public Result getPages(@PathVariable("currentPage") int currentPage,
                           @PathVariable("pageSize") int pageSize,
                           @RequestParam("authorKeywords") String authorKeywords){
        Page<Notice> page = new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<Notice> queryWrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotBlank(authorKeywords)) {
            queryWrapper.like(Notice::getAuthor, authorKeywords);
        }
        noticeService.page(page, queryWrapper);
        return Result.success(page);
    }

    @PostMapping("/addOrUpdateNotice")
    public Result addOrUpdateNotice(@RequestBody Notice notice) {
        noticeService.saveOrUpdate(notice);
        Integer id = notice.getId();
        if(StringUtils.isNotBlank(id.toString())){
            Notice updatedNotice = noticeService.getById(id);
            return Result.success(updatedNotice);
        }
        return Result.success();
    }

    @DeleteMapping("/delNotice/{id}")
    public Result delNotice(@PathVariable("id") int id) {
        noticeService.removeById(id);
        return Result.success();
    }

    @DeleteMapping("/delNoticeBatch")
    public Result delBatch(@RequestBody List<Integer> ids) {
        noticeService.removeByIds(ids);
        return Result.success();
    }

}
